#include "qinputhooks.h"
#include <QCoreApplication>
#include <QAbstractEventDispatcher>
#include <QMetaMethod>
#include <QThread>
#include <QDebug>

Q_LOGGING_CATEGORY(logQInputHooks, "QInputHooks")

QInputHooks::QInputHooks(QObject *parent) :
	QObject(parent)
{
    qRegisterMetaType<KeyEventX>("KeyEventX");
    this->initNativeHook();
}


QInputHooks::~QInputHooks()
{
}

NATIVE_INSTANCE(QInputHooks);

KeyEvent QInputHooks::keyEventFromx(KeyEventX ex)
{
    QEvent::Type type = QEvent::Type(ex["type"].toInt());
    auto wid = ex["wid"].toInt();
    auto wids = ex["wids"].toString();
    auto key = ex["key"].toInt();
    auto mods = ex["mods"].toInt();
    auto text = ex["text"].toString();
    auto time = ex["time"].toInt();
    auto nativeScanCode = ex["nativeScanCode"].toInt();
    auto nativeVirtualKey = ex["nativeVirtualKey"].toInt();

    QKeyEvent qe(type, Qt::Key(key), Qt::KeyboardModifiers(mods),
                 nativeScanCode, nativeVirtualKey, 0,
                 text, false, 1
                 );
    KeyEvent e_(qe);
    e_.wid = wid;
    e_.wids = wids;

    return e_;
}

KeyEventX QInputHooks::keyEventTox(KeyEvent &e_)
{
    KeyEvent *e = &e_;
    KeyEventX ex;

    ex["type"] = e->type();
    ex["wid"] = e->wid;
    ex["wids"] = e->wids;
    ex["key"] = e->key();
    ex["mods"] = int(e->modifiers());
    ex["text"] = e->text();
    ex["time"] = e->time;
    ex["nativeScanCode"] = e->nativeScanCode();
    ex["nativeVirtualKey"] = e->nativeVirtualKey();
    return ex;
}
